<template>
  <div class="detail-page">
    <van-nav-bar
      left-text="返回"
      @click-left="$router.back()"
      fixed
      title="面经详情"
    />
    <header class="header">
      <h1>{{articleObj.stem}}</h1>
      <p>
       {{ articleObj.createdAt }} | {{ articleObj.views }} 浏览量 | {{ articleObj.likeCount }} 点赞数
      </p>
      <p>
        <img :src="articleObj.avatar" alt="" />
        <span>{{ articleObj.creator }}</span>
      </p>
    </header>
    <main class="body" v-html="articleObj.content">
    </main>
    <div class="opt">
      <van-icon :class="{active:likeStatus}" name="like-o" @click="like(1)"/>
      <van-icon  :class="{active:startStatus}" name="star-o" @click="star(2)"/>
    </div>
  </div>
</template>

<script>
import { articleDetail, opt } from '@/api/article'
import { setIsLike, getIsLike, getIsStart, setIsStart } from '@/utils/localStorage'
export default {
  name: 'DetailPage',
  data () {
    return {
      articleObj: {},
      startStatus: false,
      likeStatus: false
    }
  },

  methods: {
    async like (value) {
      this.articleObj.likeCount = +this.articleObj.likeCount
      this.likeStatus = !this.likeStatus
      const arr = JSON.parse(getIsLike()) || []
      if (this.likeStatus) {
        arr.push({ id: this.$route.params.id, isLike: this.likeStatus })
        setIsLike(arr)
        this.articleObj.likeCount += 1
        this.$toast.success('点赞成功')
      } else {
        const index = arr.findIndex(k => k.id === this.$route.params.id)
        console.log(index)
        arr.splice(index, 1)
        setIsLike(arr)
        if (this.articleObj.likeCount > 1) {
          this.articleObj.likeCount -= 1
        }
        this.$toast.success('成功取消')
      }

      await opt(this.$route.params.id, value)
    },
    async star (value) {
      this.startStatus = !this.startStatus
      const arr = JSON.parse(getIsStart()) || []
      if (this.startStatus) {
        arr.push({ id: this.$route.params.id, isLike: this.startStatus })
        setIsStart(arr)
        this.$toast.success('收藏成功')
      } else {
        const index = arr.findIndex(k => k.id === this.$route.params.id)
        arr.splice(index, 1)
        setIsStart(arr)
        this.$toast.success('成功取消收藏')
      }
      await opt(this.$route.params.id, value)
    }
  },
  async created () {
    const res = await articleDetail(this.$route.params.id)
    this.articleObj = res.data
    console.log(JSON.parse(getIsStart()))
    if (JSON.parse(getIsLike())) {
      this.likeStatus = JSON.parse(getIsLike()).some(k => k.id === this.$route.params.id)
    }
    if (JSON.parse(getIsStart())) {
      this.startStatus = JSON.parse(getIsStart()).some(k => k.id === this.$route.params.id)
    }
  }

}
</script>

<style lang="less" scoped>
.detail-page {
  margin-top: 44px;
  overflow: hidden;
  padding: 0 15px;
  .header {
    h1 {
      font-size: 24px;
    }
    p {
      color: #999;
      font-size: 12px;
      display: flex;
      align-items: center;
    }
    img {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      overflow: hidden;
    }
  }
  .opt {
    position: fixed;
    bottom: 100px;
    right: 0;
    > .van-icon {
      margin-right: 20px;
      background: #fff;
      width: 40px;
      height: 40px;
      line-height: 40px;
      text-align: center;
      border-radius: 50%;
      box-shadow: 2px 2px 10px #ccc;
      font-size: 18px;
      &.active {
        background: #FEC635;
        color: #fff;
      }
    }
  }
}
</style>
